/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.JConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Eduardo
 */
public class C_Login extends HttpServlet {

    String username = "";
    String password = "";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, InstantiationException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            this.username = request.getParameter("username");
            this.password = request.getParameter("password");
            JConexion jc = new JConexion("");
            String query = "SELECT c.id_usuario FROM cat_usuario c WHERE c.UserName = '" + this.username + "';";
            ArrayList al = new ArrayList();
            String[] dat;
            al.clear();
            al = jc.MySQLArray("MySQLRM", "Consulta", query);
            if(!al.isEmpty()){
                dat = (String[]) al.get(0);
                query = "SELECT c.id_usuario FROM cat_usuario c WHERE c.Pass = '" + this.password + "' and c.ID_Usuario = " + Integer.parseInt(dat[0].toString()) + ";";
                al.clear();
                al = jc.MySQLArray("MySQLRM", "Consulta", query);
                if(!al.isEmpty()){
                    dat = (String[]) al.get(0);
                    if(this.username.equals("root") && this.password.equals("adminadmin")){
                        out.println("<html><head><title>Bienvenido Administrador</title></head>"
                                + "<body><center><br><br><h1>Que deseas hacer?</h1><br><br><br><br>"
                                + "<table align=\"center\"><tr><td><a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/alta_carrera.jsp\">Registrar Carrera</a></td></tr>"
                                + "<tr><td><a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/alta_caballos.jsp\">Registrar Caballo</a></td></tr></table></center></body></html>");

                    }else{
                        //out.println("<html><head><title>Bienvenido a Mowes de Plata</title><meta http-equiv=\"Refresh\" content=\"3; url=http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/Elegir_carrera.jsp\"></head>"
                          //      + "<body></body></html>");
                          response.sendRedirect("Elegir_carrera.jsp?user=" + dat[0].toString());
                    }
                }else{
                    response.sendRedirect("login.jsp?user=dif");
                }
            }else{                
                response.sendRedirect("login.jsp?user=not");
            }
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet C_Login</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet C_Login at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
